//
//  GamePlateView.m
//  DualSnake
//
//  Created by HacJune Lee on 13. 12. 6..
//  Copyright (c) 2013년 HakJune Lee. All rights reserved.
//

#import "GamePlateView.h"

@implementation GamePlateView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSettingWithGridCount];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initSettingWithGridCount];
    }
    return self;
}

-(void)initSettingWithGridCount
{
    
    _wallWidth = [[GameMaster shared]getWallWith]; //사방 벽의 가로폭
    _gridCount = [[GameMaster shared]getGridCount]; // 안에 그려질 라인의 갯수. 정방형.
    
    CGSize gridViewSize = [[GameMaster shared]getGridSize]; //벽의 폭을 제외한 실제 게임판.
    _gridWidth = gridViewSize.width/_gridCount; //그리드 한칸의 가로폭.
    
    snake = [[Snake alloc]init];
    snake.delegate = self;
    
    arr_gameItem = [[NSMutableArray alloc]init];
    
    /*
    testBlock = [[GameBlock alloc]initWithBlockType:GAMEBLOCK_WALL];
    CGPoint tmpPoint = [self convertGridNumToCGPoing:11];
    [testBlock settingWithImage:[UIImage imageNamed:@"blockImage.png"] drawRect:CGRectMake(tmpPoint.x, tmpPoint.y, _gridWidth, _gridWidth)];
    
    itemGem = [[ItemGem alloc]initWithBlockType:GAMEBLOCK_GEM];
    CGPoint tmpPoint2 = [self convertGridNumToCGPoing:23];
    [itemGem settingWithImage:[UIImage imageNamed:@"itemGem.png"] drawRect:CGRectMake(tmpPoint2.x, tmpPoint2.y, _gridWidth, _gridWidth)];
    
    
    snakeHead = [[SnakeHead alloc]initWithBlockType:GAMEBLOCK_SNAKE_HEAD];
    CGPoint tmpPoint3 = [self convertGridNumToCGPoing:200];
    [snakeHead settingWithImage:[UIImage imageNamed:@"Box_Red.png"] drawRect:CGRectMake(tmpPoint3.x, tmpPoint3.y, _gridWidth, _gridWidth)];
     */
}

-(void)reDrawCall
{
    [self setNeedsLayout];
    [self setNeedsDisplay];
    //NSLog(@"game plate reDrawCall");
}

-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    
    //게임판 라인 그리기.
    CGContextSetLineWidth(context, 1.0); //라인 굵기.
    //위에서 아래로. 세로라인.
    for (int i = 1; i < _gridCount; i++)
    {
        CGContextMoveToPoint(context, _wallWidth + (i * _gridWidth), _wallWidth); //start at this point
        CGContextAddLineToPoint(context, _wallWidth + (i * _gridWidth), self.frame.size.height - _wallWidth); //draw to this point
        
        //왼쪽에서 오른쪽. 가로라인.
        for (int j = 1; j < _gridCount; j++)
        {
            CGContextMoveToPoint(context, _wallWidth, _wallWidth + (j * _gridWidth)); //start at this point
            CGContextAddLineToPoint(context, self.frame.size.width - _wallWidth, _wallWidth + (j * _gridWidth)); //draw to this point
        }
        
    }
    //선그리기 끝
    CGContextStrokePath(context);
    
    //뱀그리기.
    [snake drawGameblock];
    
    //아이템 그리기.
    for (GameBlock * gameBlock in arr_gameItem)
    {
        [gameBlock drawGameblock];
    }
    
    /*
    [testBlock drawGameblock];
    [itemGem drawGameblock];
    [snakeHead drawGameblock];
     */
}

-(void)createItem:(int)itemType
{
    if(itemType == 0)
    {
        ItemTail * tail = [[ItemTail alloc]initWithBlockType:GAMEBLOCK_ITEM];
        
        int randomGridNum = [tail randomGridNum];
        for (GameBlock * gameBlock in [snake arr_snakeBlock])
        {
            if (gameBlock.gridNum == randomGridNum) {
                [self createItem:itemType];
                return;
            }
        }
        
        [tail settingWithImage:[UIImage imageNamed:@"Box_Orange.png"] gridNum:randomGridNum];
        [arr_gameItem addObject:tail];
    }
}





@end
